$(function () {

    initLabel();
    vm.conshow();
    vm.bindlvList();
    vm.getDeptId();
});

function showRate(agentId) {
    $.get(baseURL + "sys/sysAgent/getRateInfo/" + agentId, r => {
        if (r.code == 0) {
            for (let i = 0; i < r.rateList.length; i++) {
                vm.$set(vm.rateList, i, r.rateList[i]);
            }
            layer.open({
                type: 1,
                title: "等级列表",
                content: $("#roleList"),
                btn: ['确定'],
                btn1: function (index) {
                    layer.close(index)
                }
            })
        } else {
            layer.msg("未查询到分润信息")
        }
    })


}

function forbidAgent(aId) {
    getAgentInfo(aId);
    layer.open({
        type: 1,
        offset: '50px',
        skin: 'layui-layer-molv',
        title: "请选择封禁类别",
        area: ['300px', '250px'],
        shade: 0,
        shadeClose: false,
        content: jQuery("#freezAgent"),
        btn: ['封禁账户', '封禁提现', '取消'],
        btn1: function (index) {
            if (vm.agent.state == 2) {
                layer.msg('该账户已被封禁!');
                return;
            }
            confirm('确定要封禁该账户？', function () {
                vm.agent.state = 2;
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/sysAgent/freezAgent",
                    contentType: "application/json",
                    data: JSON.stringify(vm.agent),
                    success: function (r) {
                        if (r.code == 0) {
                            layer.msg('操作成功');
                            vm.reload();
                            layer.close(index);
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        btn2: function (index) {
            if (vm.agent.state == 3) {
                layer.msg('该账户提现已被封禁!');
                return;
            }
            confirm('确定要封禁该账户？', function () {
                vm.agent.state = 3;
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/sysAgent/freezAgent",
                    contentType: "application/json",
                    data: JSON.stringify(vm.agent),
                    success: function (r) {
                        if (r.code == 0) {
                            layer.msg('操作成功')
                            vm.reload();
                            layer.close(index);
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        }
    });
}

function toUpdate(aid) {

    vm.showList = false;
    vm.showUpdate = true;
    getAgentInfo(aid);
}

function getAgentInfo(aid) {
    $.ajax({
        type: "POST",
        url: baseURL + "sys/sysAgent/findDetailById",
        dataType: "json",
        data: {"agentId": aid},
        success: function (r) {
            vm.title = "修改";
            vm.agent = r.agent
        }
    });
}


var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            phone: null,
            name: null,
            deptId: null,
            agentId: null,
            parentName: null,
            type: 0,
            grade: 0,
            standardState:-1
        },
        showList: true,
        showUpdate: false,
        title: null,
        roleList: {},
        gradeList: [],
        agent: {
            agentId: null,
            name: null,
            deptName: null,
            thisGrade: 0,
            pid: null,
            state: null,
            totalAmount: null,
            availableAmount: null,
            idCard: null,
            agentInfoId: null,
            locked: false,
            realName: null
        },
        rateList: [],
        verify: {
            agentId: null,
            identity: null,
            bankNp: null,
            bankName: null

        }, types: [
            {value: '0', msg: "请选择角色"},
            {value: '2', msg: "普通"},
            {value: '3', msg: "盟主"}
        ],
        deptId: null

    },
    methods: {
        query: function () {
            vm.reload();
        },
        clean: function () {
            vm.showList = true;
            vm.showUpdate = false;
            vm.q = {
                phone: null,
                name: null,
                deptId: null,
                agentId: null,
                parentName: null,
                type: 0,
                grade: 0,
                standardState:-1
            };
            $("#jqGrid").jqGrid('setGridParam', {
                page: 1
            });
            vm.reload();
        },
        update: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/sysAgent/updateAgent",
                contentType: "application/json",
                data: JSON.stringify(vm.agent),
                success: function (r) {
                    if (r.code == 0) {
                        alert("成功")
                        vm.reload();
                    } else {
                        alert(r.msg)

                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            vm.showUpdate = false;
           // vm.q.deptId = $("#deptList").val();
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'phone': vm.q.phone, 'name': vm.q.name, 'deptId': vm.q.deptId, 'agentId': vm.q.agentId,
                    'parentName': vm.q.parentName, 'grade': vm.q.grade, 'type': vm.q.type,'standardState':vm.q.standardState
                },
                page: page
            }).trigger("reloadGrid");
        },
        bindlvList: function () {
            $.getJSON(baseURL + "sys/syslevelrate/list", (result) => {
                vm.$set(vm.gradeList, 0, {
                    lv: 0,
                    rate: "请选择等级"
                });

                for (let i = 0; i < result.page.length; i++) {
                    vm.$set(vm.gradeList, (i + 1), result.page[i]);
                }
            })
        },
        conshow: function () {
            $.get(baseURL + "/sys/user/info", function (r) {
                if (r.user.userId == "1") {
                    dept.created();
                    $("#showflag").show();

                }
            })
        },
        down: function () {
            window.location.href = baseURL + "sys/sysAgent/down";
        }, //获得当前登录人的渠道id
        getDeptId: function () {
            $.get(baseURL + "sys/user/info", function (r) {
                vm.deptId = r.user.deptId;
            })
        },
    }
});


var dept = new Vue({
    el: '#deptList',
    data: {
        deptId: null
    },
    methods: {
        created: function () {
            $.getJSON(baseURL + "sys/dept/list", (result) => {
                var text = "";
                for (var i = 0; i < result.length; i++) {
                    if (result[i].parentId == 0) {
                        continue;
                    }
                    text += "<option value=" + result[i].deptId + ">" + result[i].name + "</option>";
                }
                $("#deptList").append(text);
            });
        }
    }
});

function updateRealName(agentId, realName) {

    if (realName == '已认证' || realName == 1) {
        confirm("是否取消认证", function () {
            confirm("请确定要取消该合伙人实名认证,此操作会删除部分信息", function () {
                $.get(baseURL + "sys/sysAgent/cancelRealName/" + agentId, r => {
                    if (r.code == 0) {
                        alert('取消成功')
                        vm.reload();
                    } else {

                    }
                })
            })
        })
    } else {
        confirm("是否填写信息以实名认证", function () {
            showForm(agentId)
        })
    }

}

var index;

function showForm(agentId) {
    vm.verify.agentId = agentId;
    index = layer.open({
        type: 1,
        title: "实名认证",
        content: $("#verify"),
        area: ['480px', '480px'],
        btn: ['确定', '取消'],
        btn1: function () {
            uploadVerify();

        }

    })
}

function uploadVerify() {
    if (vm.verify.identity == null || vm.verify.identity.length == 0) {
        layer.msg("身份证号码不能为空");
        return;
    }
    if (vm.verify.bankName == null || vm.verify.bankName.length == 0) {
        layer.msg("银行名称不能为空");
        return;
    }
    if (vm.verify.bankNo == null || vm.verify.bankNo.length == 0) {
        layer.msg("银行卡号码不能为空");
        return;
    }

    $.ajax({
        url: baseURL + "sys/sysAgent/realName",
        data: JSON.stringify(vm.verify),
        contentType: "application/json",
        type: "post",
        success: function (r) {
            if (r.code == 0) {
                layer.msg("认证成功")
                layer.close(index);
                vm.reload();
            } else {
                layer.msg(r.msg)
            }
        }
    })

}

function initLabel() {
    $.get(baseURL + "sys/user/info", function (r) {
        if (r.user.deptId == 5) {
            inital1();
        } else {
            inital2();
        }
    })

}